<?php
	ob_start();
	session_start();
	include 'operations/connection.php';	
	function approve($idGroup,$idEmployee,$comment) {		
		$id = 0;
		$query = mysql_query("SELECT l.Order FROM listapprove l  WHERE l.IDEmployee ='$idEmployee'  AND l.IDGroupApprove = '$idGroup';");
		while ($row = mysql_fetch_array($query)){
			$id = $row[Order];
			mysql_query("UPDATE listapprove SET IDStatus=1,DateApprove=NOW(),Description='$comment'
						 WHERE IDEmployee=$idEmployee
						 AND IDGroupApprove = $idGroup");
			
			mysql_query("UPDATE list_reference SET id_status='2'
						 WHERE id_manager=$idEmployee
						 AND id_group = $idGroup");
		}
		$id=$id+1;
		$pending = 0;
		$query = mysql_query("SELECT listapprove.IDStatus FROM listapprove,approvedoc
				 	WHERE listapprove.IDGroupApprove = approvedoc.IDGroupApprove
					AND listapprove.IDGroupApprove = $idGroup");
		while ($row = mysql_fetch_array($query)) {
			if ($row[IDStatus] == 4)
				$pending++;
		}
		if ($pending == 0) {
			mysql_query("UPDATE approvedoc SET IDStatus=1 WHERE IDGroupApprove = $idGroup");
			updateDateApprove($idGroup);
		} else {
			mysql_query("UPDATE listapprove SET IDStatus=2 WHERE listapprove.IDGroupApprove=$idGroup
			AND listapprove.Order=$id");
		}	
		header("Location: received.php?content=list-receiver-not-approved-content");
	}
	
	function updateDocument($idGroup, $id) {
					
	}
	
	function updateDateApprove($idGroup) {
		mysql_query("UPDATE approvedoc SET TimeApprove = NOW() WHERE IDGroupApprove=$idGroup");		
	}
	
	function reject($idGroup,$idEmployee,$comment){
		mysql_query("UPDATE listapprove SET IDStatus=3,DateApprove=NOW(),Description='$comment' 
						WHERE IDEmployee = $idEmployee
						AND IDGroupApprove = $idGroup");
		mysql_query("UPDATE list_reference SET id_status='2'
						 WHERE id_manager=$idEmployee
						 AND id_group = $idGroup");
		mysql_query("UPDATE approvedoc SET IDStatus=3 WHERE IDGroupApprove = $idGroup");
		updateDateApprove($idGroup);
		header("Location: received.php?content=list-receiver-not-approved-content");				

	}
	
	function viewReference($idGroup,$idEmployee,$comment){
		mysql_query("update list_reference set id_status = 1 WHERE id_group=$idGroup AND id_employee=$idEmployee");		
		header("Location: received.php?content=list-receiver-reference-waiting-content");	
	}
	
	
		
	if (isset($_REQUEST['Approve'])){
		$comment=$_REQUEST['textarea'];
		approve($_SESSION['idGroup'], $_SESSION['id_employee'],$comment);	
	}
	if (isset($_REQUEST['Reject'])){
		$comment=$_REQUEST['textarea'];
		reject($_SESSION['idGroup'], $_SESSION['id_employee'],$comment);	
	}
	if (isset($_REQUEST['Accept'])){
		$comment=$_REQUEST['textarea'];
		viewReference($_SESSION['idGroup'], $_SESSION['id_employee'],$comment);	
	}	
	if (isset($_REQUEST['Delete'])){
		if($_REQUEST['type']==11){
			if($_SESSION['action']=="my_meeting_sending" ){
				$sql = "delete FROM listapprove where IDGroupApprove='".$_SESSION['idGroup']."'";
				mysql_query($sql);

				$sql = "delete FROM list_reference where id_group='".$_SESSION['idGroup']. "'";
				mysql_query($sql);

				$sql = "DELETE FROM approvedoc WHERE IDApproveDoc = '".$_REQUEST['id']."'";
				mysql_query($sql);
				header("Location: mydocument.php?content=list-mydocument-meeting-sending-content");
			}
		}else{
			$id=$_REQUEST['id'];
			mysql_query("UPDATE approvedoc SET IDStatus=6 WHERE (IDApproveDoc='$id')");	
			$daction = $_SESSION['action'];
			if($daction=="my_sending"){
				header("Location: mydocument.php?content=list-mydocument-sending-content");
			}else{
				header("Location: mydocument.php?content=list-mydocument-declined-content");
			}	
		}
		
		
	}	

	if (isset($_REQUEST['Edit'])){
		$id=$_REQUEST['id'];
		$type=$_REQUEST['type'];
		$daction = $_SESSION['action'];
		$sql = "delete FROM listapprove where IDGroupApprove='".$_SESSION['idGroup']."'";
		mysql_query($sql);
		$sql = "update approvedoc set IDGroupApprove = NULL,IDStatus = 4 where IDApproveDoc='".$id."'";
		mysql_query($sql);
		$sql = "DELETE FROM list_reference WHERE id_group = '".$_SESSION['idGroup']."'";
		mysql_query($sql);
		header("Location: newform.php?idapprovedoc=".$id."&type=".$type."&action=".$daction."");
	}	
	
	if(isset($_REQUEST['Restore'])){
		if($_SESSION['idGroup']!=""){
			$sql=mysql_query("select IDStatus from listapprove where IDGroupApprove = '".$_SESSION['idGroup']."'");
			echo "select IDStatus from listapprove where IDGroupApprove = '".$_SESSION['idGroup']."'";
			$idstatus=1;
			while($row = mysql_fetch_array($sql)){
				if($row[0]==2){
					$idstatus=2;
				}else if($row[0]==3){
					$idstatus=3;
				}
			}
			//echo $idstatus;
			$sql="update approvedoc set IDStatus = ".$idstatus." where IDGroupApprove ='".$_SESSION['idGroup']."'";
			mysql_query($sql);
		}else{
			$sql="update approvedoc set IDStatus =4 where IDApproveDoc ='".$_REQUEST['id']."'";
			mysql_query($sql);
		}
		
		header("Location: mybin.php?content=list-mybin-senddoc-content");
	}
		
	
	ob_flush();
	
?>